package com.fingard.dsp.bank.directbank.zjrb01;

import java.math.BigDecimal;
import java.util.*;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.directbank.fngrd01.util.StringHelper;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlReadMode;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/4/26.
 */
public class ZJRB01Balance extends ZJRB01Base implements IBalance {


    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        return p_req7506.isBatch?BSQueryBalListOp(p_req7506):BSQueryBalOp(p_req7506);
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }

    public RetAts7506BalQry BSQueryBalOp(Req7506BalQry p_req7506)throws Exception{
        RetAts7506BalQry retAts7506BalQry = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts7506BalQry.writeStart();
        try {
            String opName = "BSQueryBalOp";
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            writeCommenHeader(xmlTextWriter, opName);
            XElement ReqParam = new XElement("ReqParam","");
            if(p_req7506.readRecord()) {
                ReqParam.addChild(new XElement("Area_code",p_req7506.readedDetail.actArea));
                ReqParam.addChild(new XElement("Account_num",p_req7506.readedDetail.actNum));
                ReqParam.addChild(new XElement("Account_cur",convertCurToBank(p_req7506.readedDetail.actCur)));
                ReqParam.addChild(new XElement("userID",bankFront.loginName));
                ReqParam.addChild(new XElement("PackageID",getQryTransID()));
                ReqParam.addChild(new XElement("ReqReserved1",""));
                ReqParam.addChild(new XElement("ReqReserved2",""));
            }else{
                throw  new Exception("余额查询条件记录不存在");
            }
            wirteReqBody(xmlTextWriter, ReqParam, null);
            xmlTextWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlTextWriter.toXmlString());
            if(bankResult[0].length()==0){
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpSbMsg);
                if(tmpBkReader!=null){
                    if (tmpBkReader.read("/ROOT/opRep/opResult")){
                        String retCode = tmpBkReader.readedXElement.getSingleTextNotNull("retCode");
                        if("0".equals(retCode)){
                            RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.respInfo = tmpBkReader.readedXElement.getSingleText("errMsg");
                            tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("Account_num");
                            tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("Account_cur"));
                            String currBal = tmpBkReader.readedXElement.getSingleText("Balance").trim();
                            tmpRetDetail.currBal = new BigDecimal(currBal).divide(new BigDecimal("100")).setScale(2,BigDecimal.ROUND_HALF_UP).toString();
                            String availBal = tmpBkReader.readedXElement.getSingleText("Usable_balance").trim();
                            tmpRetDetail.availBal = new BigDecimal(availBal).divide(new BigDecimal("100")).setScale(2,BigDecimal.ROUND_HALF_UP).toString();
                            String yesterdayBal = tmpBkReader.readedXElement.getSingleText("Acc_balance").trim();
                            tmpRetDetail.yesterdayBal = new BigDecimal(yesterdayBal).divide(new BigDecimal("100")).setScale(2,BigDecimal.ROUND_HALF_UP).toString();
                            retAts7506BalQry.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                        }else {
                            retAts7506BalQry.writeRespCode("-1");
                            retAts7506BalQry.writeRespInfo(tmpBkReader.readedXElement.getSingleText("errMsg"));
                        }
                    }
                }else{
                    retAts7506BalQry.writeRespCode("-1");
                    retAts7506BalQry.writeRespInfo(tmpSbMsg.toString());
                }

            }else{
                retAts7506BalQry.writeRespCode("-1");
                retAts7506BalQry.writeRespInfo(bankResult[1]);
            }
        }catch (Exception ex){
            retAts7506BalQry.writeRespCode("-1");
            retAts7506BalQry.writeRespInfo(ex.getMessage());
            ex.printStackTrace();
        }
        retAts7506BalQry.writeEnd();
        return retAts7506BalQry;
    }

    public RetAts7506BalQry BSQueryBalListOp(Req7506BalQry p_req7506)throws Exception{
        RetAts7506BalQry retAts7506BalQry = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts7506BalQry.writeStart();
        try {
            String opName = "BSQueryBalListOp";
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            writeCommenHeader(xmlTextWriter, opName);
            XElement ReqParam = new XElement("ReqParam","");
            ReqParam.addChild(new XElement("userID",bankFront.loginName));
            ReqParam.addChild(new XElement("PackageID",getQryTransID()));
            ReqParam.addChild(new XElement("ReqReserved1",""));
            ReqParam.addChild(new XElement("ReqReserved2",""));
            XElement ReqParamSet =new  XElement("ReqParamSet","");
            while(p_req7506.readRecord()){
                XElement ReqParam2 = new XElement("ReqParam","");
                ReqParam2.addChild(new XElement("Area_code",p_req7506.readedDetail.actArea));
                ReqParam2.addChild(new XElement("Account_num",p_req7506.readedDetail.actNum));
                ReqParam2.addChild(new XElement("Account_cur",convertCurToBank(p_req7506.readedDetail.actCur)));
                ReqParam2.addChild(new XElement("ReqReserved3",""));
                ReqParam2.addChild(new XElement("ReqReserved4",""));
                ReqParamSet.addChild(ReqParam2);
            }
            wirteReqBody(xmlTextWriter,ReqParam,ReqParamSet);
            xmlTextWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlTextWriter.toXmlString());
            if(bankResult[0].length()==0){
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpSbMsg);
                if(tmpBkReader!=null){
                    String retCode = tmpBkReader.getFirstText("/ROOT/opRep/opResult/retCode");
                    //---0-成功1-可疑 其他为失败，错误描述在errMsg 中
                    if("0".equals(retCode)){
                        while (tmpBkReader.read("/ROOT/opRep/opResultSet/opResult")){
                            RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("Account_num");
                            tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("Account_cur"));
                            String currBal = tmpBkReader.readedXElement.getSingleText("Balance").trim();
                            tmpRetDetail.currBal = new BigDecimal(currBal).divide(new BigDecimal("100")).setScale(2,BigDecimal.ROUND_HALF_UP).toString();
                            String availBal = tmpBkReader.readedXElement.getSingleText("Usable_balance").trim();
                            tmpRetDetail.availBal = new BigDecimal(availBal).divide(new BigDecimal("100")).setScale(2,BigDecimal.ROUND_HALF_UP).toString();
                            String yesterdayBal = tmpBkReader.readedXElement.getSingleText("Acc_balance").trim();
                            tmpRetDetail.yesterdayBal = new BigDecimal(yesterdayBal).divide(new BigDecimal("100")).setScale(2,BigDecimal.ROUND_HALF_UP).toString();
                            retAts7506BalQry.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                        }
                        tmpBkReader.resetReadMode(XmlReadMode.XElementReader);
                        while (tmpBkReader.read("/ROOT/opRep/ErrSet/Err")){
                            RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo = tmpBkReader.readedXElement.getSingleText("errMessage");
                            tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("Account_num");
                            retAts7506BalQry.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                        }

                    }else if("1".equals(retCode)){
                        retAts7506BalQry.writeRespCode("-1");
                        retAts7506BalQry.writeRespInfo(tmpBkReader.getFirstText("/ROOT/opRep/opResult/errMsg"));
                    }else {
                        retAts7506BalQry.writeRespCode("-1");
                        retAts7506BalQry.writeRespInfo(tmpBkReader.getFirstText("/ROOT/opRep/opResult/errMsg"));
                    }

                }else {
                    retAts7506BalQry.writeRespCode("-1");
                    retAts7506BalQry.writeRespInfo(tmpSbMsg.toString());
                }

            }else{
                retAts7506BalQry.writeRespCode("-1");
                retAts7506BalQry.writeRespInfo(bankResult[1]);
            }

        }catch (Exception ex){
            retAts7506BalQry.writeRespCode("-1");
            retAts7506BalQry.writeRespInfo(ex.getMessage());
            ex.printStackTrace();
        }
        retAts7506BalQry.writeEnd();
        return retAts7506BalQry;
    }
}
